package com.jnevision.attendancegroup.service;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jnevision.attendancegroup.mapper.AttendanceGroupMapper;
import com.jnevision.attendancegroup.model.AttendanceGroup;
import com.jnevision.attendancegroup.model.AttendanceVo;
import com.jnevision.laterule.model.LateRule;
import com.jnevision.laterule.service.LateRuleServiceImpl;
import com.jnevision.leaveearlyrule.model.LeaveEarlyRule;
import com.jnevision.leaveearlyrule.service.LeaveEarlyRuleServiceImpl;
import com.jnevision.personalattendance.model.PersonalAttendance;
import com.jnevision.personalattendance.service.PersonalAttendanceServiceImpl;

/**
 * <p>
 * 考勤组 服务实现类
 * </p>
 *
 * @author gavin
 * @since 2019-07-21
 */
@Service
public class AttendanceGroupServiceImpl extends ServiceImpl<AttendanceGroupMapper, AttendanceGroup> {

    @Autowired
    private LateRuleServiceImpl lateRuleServiceImpl;

    @Autowired
    private LeaveEarlyRuleServiceImpl leaveEarlyRuleServiceImpl;

    @Autowired
    private PersonalAttendanceServiceImpl personalAttendanceServiceImpl;

    public Boolean saveAttendance(AttendanceVo vo) {
        Boolean flag = true;

        LateRule lateRule = new LateRule();
        lateRule.setGroupId(vo.getGroupId());
        lateRule.setModel(vo.getLateRuleModel());
        lateRule.setMoney(vo.getLateRuleMoney());
        lateRule.setExempt(vo.getLaterRuleExempt());

        LateRule one = lateRuleServiceImpl.getOne(new QueryWrapper<LateRule>().eq("group_id", vo.getGroupId()));
        if (one != null) {
            lateRule.setId(one.getId());
        }

        LeaveEarlyRule leaveEarlyRule = new LeaveEarlyRule();
        leaveEarlyRule.setGroupId(vo.getGroupId());
        leaveEarlyRule.setModel(vo.getLeaveEarlyModel());
        leaveEarlyRule.setMoney(vo.getLeaveEarlyMoney());
        leaveEarlyRule.setExempt(vo.getLeaveEarlyExempt());

        LeaveEarlyRule one2 =
            leaveEarlyRuleServiceImpl.getOne(new QueryWrapper<LeaveEarlyRule>().eq("group_id", vo.getGroupId()));

        if (one2 != null) {
            leaveEarlyRule.setId(one2.getId());
        }

        AttendanceGroup attendanceGroup = new AttendanceGroup();
        BeanUtils.copyProperties(vo, attendanceGroup);
        attendanceGroup.setId(vo.getGroupId());

        flag &= this.retBool(this.baseMapper.updateById(attendanceGroup));
        flag &= lateRuleServiceImpl.saveOrUpdate(lateRule);
        flag &= leaveEarlyRuleServiceImpl.saveOrUpdate(leaveEarlyRule);

        return flag;
    }

    public List<AttendanceVo> listByDeptId(Integer deptId) {

        return this.baseMapper.listByDeptId(deptId);
    }

    @Transactional
    public Boolean deleteById(Integer id) {
        Boolean flag = true;
        flag &= lateRuleServiceImpl.remove(new QueryWrapper<LateRule>().eq("group_id", id));
        flag &= leaveEarlyRuleServiceImpl.remove(new QueryWrapper<LeaveEarlyRule>().eq("group_id", id));
        flag &= personalAttendanceServiceImpl.remove(new QueryWrapper<PersonalAttendance>().eq("group_id", id));
        return this.retBool(this.baseMapper.deleteById(id));
    }

    public List<AttendanceVo> listByGroupId(Integer id) {
        return this.baseMapper.listByGroupId(id);
    }

}
